package com.example.DataSource.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.DataSource.Mapper.CMMapper;
import com.example.DataSource.Service.CMService;
import com.example.DataSource.entity.ChatMessage;
import com.example.DataSource.entity.consultant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CMServiceImpl extends ServiceImpl<CMMapper, ChatMessage> implements CMService {
    @Override
    @Transactional
    public IPage<ChatMessage> getCMPage(Page<ChatMessage> page) {
        QueryWrapper<ChatMessage> queryWrapper = new QueryWrapper<>();
        IPage<ChatMessage> CMPage = this.page(page,queryWrapper);
        List<ChatMessage> records = CMPage.getRecords();
//        需要根据咨询师编号去查询咨询师name
        for (ChatMessage CM :
                records) {
            String consultantName = getConsultantName(CM.getConsultant_id());
            CM.setConsultantName(consultantName);
        }
        return CMPage;
    }

    @Override
    @Transactional
    public IPage<ChatMessage> getCMPageSearch(Page<ChatMessage> page, String searchInfo) {
        QueryWrapper<ChatMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("content", searchInfo)
                .or()
                .like("reply_content", searchInfo);
        IPage<ChatMessage> CMPage = this.page(page,queryWrapper);
        List<ChatMessage> records = CMPage.getRecords();
//        需要根据咨询师编号去查询咨询师name
        for (ChatMessage CM :
                records) {
            String consultantName = getConsultantName(CM.getConsultant_id());
            CM.setConsultantName(consultantName);
        }
        return CMPage;
    }

    @Autowired
    private consultantServiceImpl consultantService;
    private String getConsultantName(int caID){
        QueryWrapper<consultant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",caID);
        return consultantService.list(queryWrapper).get(0).getName();
    }
}
